package com.ming.elm.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ming.elm.dao.CartDao;
import com.ming.elm.dao.OrderDetailetDao;
import com.ming.elm.dao.OrdersDao;
import com.ming.elm.po.Cart;
import com.ming.elm.po.OrderDetailet;
import com.ming.elm.po.Orders;
import com.ming.elm.service.OrdersService;
import com.ming.elm.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

public class OrdersServiceImpl implements OrdersService{

	@Override
	public int createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		// 创建Orders的dao层对象
		OrdersDao odMapper = sqlSession.getMapper(OrdersDao.class);
		// 创建Cart的dao层对象
		CartDao cdMapper = sqlSession.getMapper(CartDao.class);
		// 创建OrderDetailet的Dao层对象
		OrderDetailetDao oddMapper = sqlSession.getMapper(OrderDetailetDao.class);

		// 订单创建日期
		Date cur = new Date();
		// 调用日期格式化工具类
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str_cur = sdf.format(cur);

		// 封装订单数据
		Orders data = new Orders();
		data.setBusinessId(businessId);
		data.setOrderDate(str_cur);
		data.setUserId(userId);
		data.setDaId(daId);
		data.setOrderTotal(orderTotal);

		// 定义返回的订单编号
		int orderId = -1;
		try {
			odMapper.saveOrders(data);
			// // 封装购物车查询数据
			// Cart cart = new Cart();
			// cart.setBusinessId(businessId);
			// cart.setUserId(userId);
			// // 查询购物车的数据
			// List<Cart> clist = cdMapper.listCart(cart);
			// // 创建订单明细集合
			// List<OrderDetailet> odlist = new ArrayList<>();
			// // 遍历购物车集合,把购物车数据转换为订单明细数据
			// for (Cart c : clist) {
			// 	Integer foodId = c.getFoodId();
			// 	Integer quantity = c.getQuantity();
			// 	// 每轮循环创建一个创建订单明细对象
			// 	OrderDetailet orderDetailet = new OrderDetailet();
			// 	// 传递订单主键
			// 	orderDetailet.setFoodId(foodId);
			// 	// 传递食品数量
			// 	orderDetailet.setQuantity(quantity);
			// 	// 传递订单编号属性
			// 	orderDetailet.setOrderId(orderId);
			// 	// 把封装了订单明细数据的对象添加到订单明细列表里
			// 	odlist.add(orderDetailet);
			// }
			// // 订单明细列表保存到数据库
			// int result = oddMapper.saveOrderDetailetBatch(odlist);

			// 返回订单编号Id
			orderId = data.getOrderId();

			// 生成订单明细数据表
			int result = oddMapper.saveOrderDetailet(orderId);
			if(result>0){
				System.out.println("生成订单明细数据成功");
			}
			// 清空购物车
			Cart cart = new Cart();
			cart.setUserId(userId);
			cart.setBusinessId(businessId);
			cdMapper.removeCart(cart);
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			// 一旦try代码块里出现异常,orderId需要回到-1
			orderId = -1;
		} finally {
			sqlSession.commit();
			sqlSession.close();
		}


		return orderId;
	}
	
	@Override
	public Orders getOrdersById(Integer orderId) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		OrdersDao mapper = sqlSession.getMapper(OrdersDao.class);
		Orders orders = null;
		try {
			orders = mapper.getOrdersById(orderId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}

		return orders;
	}
	
	@Override
	public List<Orders> listOrdersByUserId(String userId){
		List<Orders> list = null;
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		OrdersDao mapper = sqlSession.getMapper(OrdersDao.class);
		try {
			list = mapper.listOrdersByUserId(userId);
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.close();
		}

		return list;
	}

	@Override
	public List<Orders> listOrdersDetailByUserId(String userId) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		OrdersDao mapper = sqlSession.getMapper(OrdersDao.class);

		List<Orders> list = null;
		try {
			list = mapper.listOrdersByUserId(userId);
			if(list.size()>0){
				for (int i = 0; i < list.size(); i++) {
					Integer orderId = list.get(i).getOrderId();
					Orders order = this.getOrdersById(orderId);
					list.set(i,order);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.close();
		}

		return list;
	}
}
